package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemCatMapper;
import com.usian.mapper.TbItemDescMapper;
import com.usian.mapper.TbItemDescMapper;
import com.usian.mapper.TbItemMapper;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.pojo.*;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbItemDesc;
import com.usian.pojo.TbItemParamItem;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ItemServiceImpl implements ItemService {
	//商品
	@Autowired
	private TbItemMapper tbItemMapper;
	//商品描述
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	//商品类目
	@Autowired
	private TbItemCatMapper tbItemCatMapper;
	//商品规格信息
	@Autowired
	private TbItemParamItemMapper tbItemParamItemMapper;

	@Override
	public TbItem selectItemInfo(Long itemId) {
		return tbItemMapper.selectByPrimaryKey(itemId);
	}

	/**
	 * shangxingming
	 * 查询商品列表接口
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
		PageHelper.startPage(page,rows);
		List<TbItem> tbItemList= tbItemMapper.selectByExample(null);
		PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
		PageResult pageResult = new PageResult();
		pageResult.setPageIndex(pageInfo.getPageNum());
		pageResult.setTotalPage(pageInfo.getTotal());
		pageResult.setResult(pageInfo.getList());
		return pageResult;
	}

	/**
	 * 冯果果
	 * 根据Id删除商品
	 * @param itemId
	 */
	@Override
	public int deleteByPrimaryKey(Long itemId) {
		return tbItemMapper.deleteByPrimaryKey(itemId);
	}


	/**
	 * 根据商品 ID 查询商品，商品分类，商品描述，商品规格参数
	 * @param itemId
	 * @return
	 */
	@Override
	public Map<String, Object> preUpdateItem(Long itemId) {
		Map<String, Object> map = new HashMap<>();
		//根据商品 ID 查询商品
		TbItem item = this.tbItemMapper.selectByPrimaryKey(itemId);
		map.put("item", item);
		//根据商品 ID 查询商品描述
		TbItemDesc itemDesc = this.tbItemDescMapper.selectByPrimaryKey(itemId);
		map.put("itemDesc", itemDesc.getItemDesc());
		//根据商品 ID 查询商品类目
		TbItemCat itemCat = this.tbItemCatMapper.selectByPrimaryKey(item.getCid());
		map.put("itemCat", itemCat.getName());
		//根据商品 ID 查询商品规格信息
		TbItemParamItemExample example = new TbItemParamItemExample();
		TbItemParamItemExample.Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> list =
				this.tbItemParamItemMapper.selectByExampleWithBLOBs(example);
		if (list != null && list.size() > 0) {
			map.put("itemParamItem", list.get(0).getParamData());
		}
		return map;
	}

	/**
	 * 薛丁豪22.03.24
	 * 修改商品信息地址
	 * 修改的保存
	 * @param tbItem
	 * @param desc
	 * @param itemParams
	 * @return
	 */
	@Override
	public void updateTbItem(TbItem tbItem, String desc, String itemParams) {
		//补齐Tbitem数据
		Long itemId = tbItem.getId();
		this.tbItemMapper.updateByPrimaryKeySelective(tbItem);
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(itemId);
		tbItemDesc.setItemDesc(desc);
		this.tbItemDescMapper.updateByPrimaryKey(tbItemDesc);
		//补齐商品规格参数
		TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
		TbItemParamItemExample.Criteria criteria = tbItemParamItemExample.createCriteria();
		criteria.andIdEqualTo(itemId);
		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setParamData(itemParams);
		this.tbItemParamItemMapper.updateByExampleSelective(tbItemParamItem, tbItemParamItemExample);
	}



	/**
	 * 刘蕊
	 * 商品添加接口
	 * @param tbItem
	 * @param desc
	 * @param itemParams
	 * @return
	 */
	@Override
	public Integer insertTbItem(TbItem tbItem, String desc, String itemParams) {
		//补充数据
		long itemId = IDUtils.genItemId();
		tbItem.setId(itemId);
		tbItem.setStatus((byte)1);
		tbItem.setUpdated(new Date());
		tbItem.setCreated(new Date());
		tbItem.setPrice(tbItem.getPrice()*100);
		Integer tbItemNum = tbItemMapper.insertSelective(tbItem);

		//补齐商品的表述对象
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(itemId);
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setUpdated(new Date());
		tbItemDesc.setCreated(new Date());
		Integer tbitemDescNum = tbItemDescMapper.insertSelective(tbItemDesc);

		//补齐参数
		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(itemId);
		tbItemParamItem.setParamData(itemParams);
		tbItemParamItem.setUpdated(new Date());
		tbItemParamItem.setCreated(new Date());
		Integer itemParamItmeNum = tbItemParamItemMapper.insertSelective(tbItemParamItem);
		return tbItemNum + tbitemDescNum + itemParamItmeNum;
	}
}

